package com.yanqu.road.dao.impl.activity.liondance;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.liondance.CrossLionDanceUserProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossLionDanceUserProcessDataDaoImpl extends TempDataObjectDao<CrossLionDanceUserProcessData> {


    public static String getCrossLionDanceUserProcessDataSql = "select * from `t_c_activity_lion_dance_user_data` where `activityId`=?";
    public Map<Long, CrossLionDanceUserProcessData> getCrossLionDanceUserProcessData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossLionDanceUserProcessData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossLionDanceUserProcessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossLionDanceUserProcessData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossLionDanceUserProcessDataSql = "update `t_c_activity_lion_dance_user_data` set makeNum=?,process=?,updateTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossLionDanceUserProcessData crossLionDanceUserProcessData) {
        crossLionDanceUserProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceUserProcessData.getMakeNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceUserProcessData.getProcess()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceUserProcessData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceUserProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceUserProcessData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossLionDanceUserProcessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossLionDanceUserProcessDataSql = "insert into `t_c_activity_lion_dance_user_data`(activityId,userId,makeNum,process,updateTime) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CrossLionDanceUserProcessData crossLionDanceUserProcessData) {
        crossLionDanceUserProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceUserProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceUserProcessData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceUserProcessData.getMakeNum()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLionDanceUserProcessData.getProcess()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLionDanceUserProcessData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertCrossLionDanceUserProcessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossLionDanceUserProcessData parseData(ResultSet rs) throws SQLException {
        CrossLionDanceUserProcessData data = new CrossLionDanceUserProcessData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setMakeNum(rs.getInt("makeNum"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setProcess(rs.getInt("process"));
        return data;
    }
}
